﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System;
using RenderHeads.Media.AVProVideo;

public class SelectPanelScript : MonoBehaviour
{
    public GameObject StartBtn;
    public GameObject Cube;
    public MediaPlayer MediaPlayer;
    public UILabel NameLabel;
    public UILabel mSpeedValue;
    public MoviePlayerScript PlayPanel; 

    private int mCurrentSelectIndex = 0;
    private List<FileInfo> mFileInfos;
    private float mTotalRotateTime = 0.5f;
    private float mDirection = 0f;

    private int mCurrentSelectSpeedIndex = 0;
    private List<float> mSpeedList = new List<float>() { 1f, 1.1f, 1.2f, 1.3f };
    void Start()
    {
        mFileInfos = new List<FileInfo>(new DirectoryInfo(Application.streamingAssetsPath).GetFiles("*.mp4"));
        UIEventListener.Get(StartBtn).onClick += OnBtnClick;
        Refresh();
    }
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Q) || Input.GetKeyUp(KeyCode.Joystick1Button6))
        {
            mCurrentSelectIndex--;
            if (mCurrentSelectIndex < 0)
            {
                mCurrentSelectIndex += mFileInfos.Count;
            }
            mDirection = -1f;
            Select(mCurrentSelectIndex);
        }
        if (Input.GetKeyUp(KeyCode.E) || Input.GetKeyUp(KeyCode.Joystick1Button7))
        {
            mCurrentSelectIndex++;
            if (mCurrentSelectIndex >= mFileInfos.Count)
            {
                mCurrentSelectIndex -= mFileInfos.Count;
            }
            mDirection = 1f;
            Select(mCurrentSelectIndex);
        }
        if (Input.GetKeyUp(KeyCode.Z) || Input.GetKeyUp(KeyCode.Joystick1Button4))
        {
            mCurrentSelectSpeedIndex--;
            if (mCurrentSelectSpeedIndex < 0)
            {
                mCurrentSelectSpeedIndex += mSpeedList.Count;
            }
            mDirection = 0f;
            SelectSpeed(mCurrentSelectSpeedIndex);
        }
        if (Input.GetKeyUp(KeyCode.C) || Input.GetKeyUp(KeyCode.Joystick1Button5))
        {
            mCurrentSelectSpeedIndex++;
            if (mCurrentSelectSpeedIndex >= mSpeedList.Count)
            {
                mCurrentSelectSpeedIndex -= mSpeedList.Count;
            }
            mDirection = 0f;
            SelectSpeed(mCurrentSelectSpeedIndex);
        }
        if (Input.GetKeyUp(KeyCode.Joystick1Button9))
        {
            OnBtnClick(StartBtn);
        }
    }
    public void OnBtnClick(GameObject go)
    {
        if (go == StartBtn)
        {
            gameObject.SetActive(false);
            PlayPanel.gameObject.SetActive(true);
            MediaPlayer.Control.Seek(0f);
        }
    }
    public void Refresh()
    {
        mDirection = 0f;
        SelectSpeed(mCurrentSelectSpeedIndex);
    }
    private void SelectSpeed(int selectIndex)
    {
        mCurrentSelectSpeedIndex = selectIndex;
        mSpeedValue.text = "当前速度：" + mSpeedList[selectIndex].ToString();
        if (mSpeedList[selectIndex] == 1.3f)
        {
            mSpeedValue.text += "\n你咋不上天呢";
        }
        Select(mCurrentSelectIndex);
    }
    private void Select(int selectIndex)
    {
        mCurrentSelectIndex = selectIndex;
        StartCoroutine(FadeToNewMovie());
        NameLabel.text = mFileInfos[mCurrentSelectIndex].Name;
    }
    private IEnumerator FadeToNewMovie()
    {
        if (mDirection != 0f)
        {
            float startTime = Time.time;
            float percent = (Time.time - startTime) / mTotalRotateTime;
            while (percent < 1)
            {
                float yAngle = 0f;
                if (mDirection == 1f)
                {
                    yAngle = Mathf.Lerp(180f, 270f, percent);
                }
                else if (mDirection == -1f)
                {
                    yAngle = Mathf.Lerp(180f, 90f, percent);
                }
                Cube.transform.rotation = Quaternion.Euler(0f, yAngle, 0f);
                percent = (Time.time - startTime) / mTotalRotateTime;
                yield return null;
            }
        }
        Cube.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
        Cube.SetActive(false);
        MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, mFileInfos[mCurrentSelectIndex].Name);
        yield return new WaitForSeconds(0.1f);
        MediaPlayer.Control.Seek(10 * 1000); //快进10秒
        MediaPlayer.Control.SetPlaybackRate(mSpeedList[mCurrentSelectSpeedIndex]);
        Cube.SetActive(true);
    }
}
